package com.google.code.sharecontent.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.google.code.sharecontent.dao.PlaylistContentDao;
import com.google.code.sharecontent.model.PlaylistContent;

public class PlaylistContentDaoImpl extends BaseDaoImpl<PlaylistContent> implements PlaylistContentDao {

	public PlaylistContentDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	protected PreparedStatement putPrepareStatement(PreparedStatement pStat,
			PlaylistContent playlist) throws SQLException {
			pStat.setInt(1, playlist.getUserId());
			pStat.setInt(2, playlist.getContentId());
			pStat.setInt(3, playlist.getPlaylistId());
			pStat.setInt(4, playlist.getOrder());
			pStat.setTimestamp(5, new Timestamp(playlist.getDateCreated().getTime()));
		return pStat;
	}

	@Override
	protected String generateInsertSql(PlaylistContent playlist) {
		return "insert into "+getTableName()+" (USER_ID,CONTENT_ID,PLAYLIST_ID,ORDER_CONTENT,DATE_CREATED) values " +
				"(?,?,?,?,?)";
/*		return "insert into "+getTableName()+" (USER_ID,CONTENT_ID,PLAYLIST_ID,ORDER_CONTENT,DATE_CREATED) values (" +
		""+playlist.getUserId()+","+playlist.getContentId()+","+playlist.getPlaylistId()+","+
		playlist.getOrder()+", " + getTIMESTAMPWithQuotesOrNull(playlist.getDateCreated())+")";*/
	}
	
	@Override
	protected String generateUpdateSql(PlaylistContent payment) {
		return "update "+getTableName()+" set " +
		"USER_ID = ?," +
		"CONTENT_ID = ?," +
		"PLAYLIST_ID = ?," +
		"ORDER_CONTENT = ?," +
		"DATE_CREATED = ?," +
		" where ID = "+ payment.getId();
	}
	
	@Override
	public PlaylistContent createByRow(ResultSet rs) throws SQLException {
		PlaylistContent playlistContent = new PlaylistContent();
		playlistContent.setId(rs.getInt("ID"));
		playlistContent.setUserId(rs.getInt("USER_ID"));
		playlistContent.setContentId(rs.getInt("CONTENT_ID"));
		playlistContent.setPlaylistId(rs.getInt("PLAYLIST_ID"));
		playlistContent.setDateCreated(rs.getDate("DATE_CREATED"));
		playlistContent.setOrder(rs.getInt("ORDER_CONTENT"));
		return playlistContent;
	}

	@Override
	protected String getTableName() {
		return "PLAYLIST_CONTENTS";
	}

	public List<PlaylistContent> findByPlaylistId(Integer playlistId) throws SQLException {
		return list(" PLAYLIST_ID = "+playlistId, null);
	}

}
